package com.example.netty.ui.model

import android.util.Log
import androidx.lifecycle.ViewModel
import com.example.netty.constants.NettyConstants
import com.example.netty.ui.state.ConnectState
import com.lu.netty.data.NettyMessageVo
import com.lu.netty.impl.BaseNettyClient
import com.lu.netty.io.core.IoState
import com.lu.netty.io.core.NettyConnector
import com.lu.netty.io.dispatcher.INettyClientListener
import com.lu.netty.io.dispatcher.IStateListener
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
import kotlinx.coroutines.flow.update

class ClientModel : ViewModel() {

    companion object {
        private const val TAG = "ClientModel"
    }

    private val _connectState = MutableStateFlow(ConnectState.DISCONNECT)
    val connectState = _connectState.asStateFlow()

    private val nettyClient by lazy {
        BaseNettyClient(
            "0.0.0.0",
            NettyConstants.port,
        ).also {
            it.setOnStateChangeListener(object : IStateListener {
                override fun onStateChange(state: IoState) {
                    Log.i(TAG, "onStateChange: state=$state")
                    when (state) {
                        IoState.CONNECTED -> {
                            _connectState.update { ConnectState.CONNECTED }
                        }

                        IoState.CONNECTING -> {
                            _connectState.update { ConnectState.CONNECTING }
                        }

                        else -> {
                            _connectState.update { ConnectState.DISCONNECT }
                        }
                    }
                }
            })
            it.setListener(object : INettyClientListener {
                override fun onContent(connector: NettyConnector, messageVo: NettyMessageVo) {
                    Log.d(TAG, "onContent: ${messageVo.message}")
                }

                override fun onOther(connector: NettyConnector, messageVo: NettyMessageVo) {

                }

                override fun getHeartbeat(): String {
                    return "心跳1"
                }
            })
        }
    }


    private val nettyClient2 by lazy {
        BaseNettyClient(
            "0.0.0.0",
            NettyConstants.port,
        ).also {
            it.setOnStateChangeListener(object : IStateListener {
                override fun onStateChange(state: IoState) {
                    Log.i(TAG, "onStateChange2: state=$state")
                    when (state) {
                        IoState.CONNECTED -> {
                            _connectState.update { ConnectState.CONNECTED }
                        }

                        IoState.CONNECTING -> {
                            _connectState.update { ConnectState.CONNECTING }
                        }

                        else -> {
                            _connectState.update { ConnectState.DISCONNECT }
                        }
                    }
                }
            })
            it.setListener(object : INettyClientListener {
                override fun onContent(connector: NettyConnector, messageVo: NettyMessageVo) {
                    Log.d(TAG, "onContent2: ${messageVo.message}")
                }

                override fun onOther(connector: NettyConnector, messageVo: NettyMessageVo) {

                }

                override fun getHeartbeat(): String {
                    return "心跳2"
                }
            })
        }
    }

    private val clientList = arrayListOf(nettyClient, nettyClient2)

    fun startClient() {
        clientList.forEach {
            it.start()
        }
    }

    fun sendMessage(message: String) {
        clientList.forEachIndexed { i, client ->
            client.send("Client-$i : $message")
        }
    }

    fun stopClient() {
        clientList.forEach {
            it.close()
        }
    }
}